<template>
	<view>
		<view class="mp-header">
			<view class="sys-head" view :style="{ height: statusBarHeight }"></view>
			<view class="serch-wrapper flex acea-row">
				<view class="logo flex item" style="width: 33%;">
					<image class="logo" src="/static/images/logo.png"></image>
				</view>
				<view class="title item">淘见美</view>
				<!-- 占位小程序胶囊按钮 -->
				<view class="item"></view>
			</view>
			<view class="serch-box" view style="height: 35px;">
				<view class="serch-wrapper flex">
					<navigator 
						url="/pages/columnGoods/goods_search/index" 
						class="uninput" 
						hover-class="none">
						<text class="iconfont icon-xiazai5"></text>
						搜索商品
					</navigator>
					<view class="flex locat" style="align-items: center;">
						<image src="/static/images/location.png" mode="" style="width: 45rpx; margin-right: 10rpx;"></image>
						<text style="color: #eee; font-size: 24rpx; margin-left: 10rpx;">{{ currentLocation }}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="bg_area">
			<!-- 热搜 -->
			<view class="hot-search" :style="{marginTop:marTop+'px'}">
				<text class="title">热搜</text>
				<view class="scroll-content">
					<scroll-view class="scroll-wapper" :scroll-x="true">
						<text class="hot_tag">炒菜</text>
						<text class="hot_tag">麻辣香锅</text>
						<text class="hot_tag">炒菜</text>
						<text class="hot_tag">麻辣香锅</text>
						<text class="hot_tag">麻辣香锅</text>
						<text class="hot_tag">麻辣香锅</text>
						<text class="hot_tag">麻辣香锅</text>
						<text class="hot_tag">麻辣香锅</text>
					</scroll-view>
				</view>
			</view>
			<!-- 轮播 -->
			<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="5000" circular indicator-active-color="#fff">
			    <swiper-item>
			        <view class="swiper-item">
						<image src="/static/images/images/banner_03.jpg" mode="widthFix"></image>
					</view>
			    </swiper-item>
			    <swiper-item>
			        <view class="swiper-item">
						<image src="/static/images/images/banner_03.jpg" mode="widthFix"></image>
					</view>
			    </swiper-item>
			    <swiper-item>
			        <view class="swiper-item">
						<image src="/static/images/images/banner_03.jpg" mode="widthFix"></image>
					</view>
			    </swiper-item>
			</swiper>
			<!-- menu -->
			<view class='nav acea-row' v-if="menus.length">
				<block v-for="(item,index) in menus" :key="index">
					<view class="item" @click="goMenuDetail(item)">
						<view class='pictrue'>
							<image :src='item.pic'></image>
						</view>
						<view class="menu-txt area-row">{{item.name}}</view>
					</view>
				</block>
			</view>
		</view>
		<view class="wapper">
			<!-- 组合菜单 -->
			<view class="menu-group">
				<view class="top_menu">
					<view class="menu_items">
						<view class="title">
							<text style="color: #e94c36;">领取券中心</text>
							<text>限时好券限时领</text>
						</view>
						<image src="../../static/images/images/m2.png" mode=""></image>
					</view>
					<view class="menu_items">
						<view class="title">
							<text style="color: #5698ee;">排行榜</text>
							<text>哪里好咱去哪</text>
						</view>
						<image src="../../static/images/images/m1.png" mode=""></image>
					</view>
				</view>
				<view class="bottom_menu">
					<view class="menu_item">
						<text>今日特价</text>
						<image src="../../static/images/images/m3.png" mode=""></image>
					</view>
					<view class="menu_item">
						<text>金冠好店</text>
						<image src="../../static/images/images/m4.png" mode=""></image>
					</view>
					<view class="menu_item">
						<text>大牌优选</text>
						<image src="../../static/images/images/m5.png" mode=""></image>
					</view>
					<view class="menu_item">
						<text>好评如潮</text>
						<image src="../../static/images/images/m6.png" mode=""></image>
					</view>
				</view>
			</view>
			<!-- 店铺 -->
			<view class="store-wapper">
				<view class="tabs">
					<text 
						:class="curTab == index ? 'active' : ''" 
						v-for="(item,index) in tabList" 
						:key="index"
						@click="tabClick(index)"
					>{{ item.name }}</text>
				</view>
				<view class="hot-search">
					<text class="title">附近</text>
					<view class="scroll-wapper">
						<text class="hot_tag">首单立减</text>
						<text class="hot_tag">好评优选</text>
						<text class="hot_tag">预订团购</text>
						<text class="hot_tag">预订团购</text>
					</view>
				</view>
				<view class="store-list">
					<view class="store_item" v-for="(item,index) in brandList" :key="index">
						<view class="details">
							<view class="left">
								<view class="title">
									<text class="line2">{{ item.mer_name }}</text>
									<image src="/static/images/images/jinpai.png" mode=""></image>
								</view>
								<view class="rate">
									<uni-rate 
									:touchable="false" 
									:disabled="true" 
									color="#ccc" 
									disabledColor="#ff943d"
									:is-fill="false" 
									:size="14"
									:value="item.service_score"/>
								</view>
								<view class="distance">
									<text>大致商圈位置</text>
									<view class="locat">
										<image src="/static/images/images/add2.png" mode=""></image>
										<text>{{ Math.ceil(item.distance) }}km</text>
									</view>
								</view>
								<view class="fav">
									<text>{{ item.care_count }}人关注</text>
									<text class="coupon">无门槛券7元</text>
								</view>
								<view class="comments">
									<image src="/static/images/images/pinglun.png" mode=""></image>
									<text>123条评论</text>
								</view>
							</view>
							<view class="right_img" @click="goStore(item.mer_id)">
								<image :src="item.mer_avatar" mode="widthFix"></image>
							</view>
						</view>
						<view class="tags">
							<image src="/static/images/images/biaoqian.png" mode=""></image>
							<text>麻辣烫</text>
							<text>预定</text>
							<text>美食</text>
							<text>杨国福</text>
							<text>美食</text>
							<text>杨国福</text>
						</view>
						<block v-if="item.recommend.length > 0">
						<navigator class="price" :url="`/pages/goods_details/index?id=${item.recommend[0].product_id}`">
							<text style="color: #f43636;">￥{{ item.recommend[0].price }}</text>
							<del>￥{{item.recommend[0].ot_price}}</del>
							<view>
								<text class="tips">{{ item.recommend[0].store_name }}</text><text class="iconfont icon-xiangyou"></text>
							</view>
						</navigator>
						<view class="jifen">
							<image src="/static/images/images/jifen.png" mode=""></image>
							<text>预计收入{{item.recommend[0].coin}}金币</text>
						</view>
						</block>
						<view class="more" @click="goStore(item.mer_id)">查看更多优惠内容<text class="iconfont icon-xiangyou"></text></view>
					</view>
					<!-- <view class="store_item">
						<view class="details">
							<view class="left">
								<view class="title">
									<text class="line2">重庆杨国福麻辣烫香格里拉店</text>
									<image src="/static/images/images/jinpai.png" mode=""></image>
								</view>
								<view class="rate">
									<uni-rate 
									:touchable="false" 
									:disabled="true" 
									color="#ccc" 
									disabledColor="#ff943d"
									:is-fill="false" 
									:size="14"
									:value="2"/>
								</view>
								<view class="distance">
									<text>大致商圈位置</text>
									<view class="locat">
										<image src="/static/images/images/add2.png" mode=""></image>
										<text>4.2km</text>
									</view>
								</view>
								<view class="fav">
									<text>24人收藏</text>
									<text class="coupon">无门槛券7元</text>
								</view>
								<view class="comments">
									<image src="/static/images/images/pinglun.png" mode=""></image>
									<text>123条评论</text>
								</view>
							</view>
							<view class="right_img">
								<image src="/static/images/images/storeUrl_07.jpg" mode="widthFix"></image>
							</view>
						</view>
						<view class="more">查看更多优惠内容<text class="iconfont icon-xiangyou"></text></view>
					</view> -->
				</view>
				<view class='loadingicon acea-row row-center-wrapper' v-if='brandList.length > 0'>
					<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	import {
		getIndexData
	} from '@/api/api.js';
	import {
		storeMerchantList
	} from '@/api/store.js';
	export default {
		data() {
			return {
				statusBarHeight: statusBarHeight,
				currentLocation: '',
				logoUrl: '',
				marTop: 0,
				menus: [],
				tabList: [
					{
						name: '全程精选'
					},
					{
						name: '特色美食'
					},
					{
						name: '宝藏玩乐'
					}
				],
				curTab: 0,
				brandList: [],
				page: 1,
				limit: 5,
				loading: false,
				loadTitle: '加载更多',
				loadend: false,
				lng: '',
				lat: ''
			}
		},
		onLoad() {
			Promise.all([this.getCurLoaction(), this.getIndexConfig()])
		},
		onReady() {
			let info = uni.createSelectorQuery().select(".mp-header");
			info.boundingClientRect((data) => {
				this.marTop = data.height
			}).exec()
		},
		methods: {
			// 获取当前所在位置
			getCurLoaction() {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					success: function(res) {
						let latitude,longitude;
						latitude = res.latitude.toString();
						longitude = res.longitude.toString();
						that.lat = res.latitude
						that.lng = res.longitude
						that.storeMerchant()
						uni.request({
							header:{
								"Content-Type": "application/text"
							},
							url:'https://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key=QGABZ-CAECX-UNQ4B-7JDNS-LPOLZ-M3FDL',
							success(re) {
								if(re.statusCode===200){
									that.currentLocation = re.data.result.address_component.city
								}else{
									uni.showToast({
										title: '获取信息失败，请重试！',
										icon: 'none'
									})
								}
							 }
						});
					}
				});
			},
			// 菜单数据
			getIndexConfig: function() {
				let that = this;
				getIndexData().then(res => {
					that.$set(that, "menus", res.data.menu);
				})
			},
			
			// 菜单详情
			goMenuDetail(item) {
				if (item.url == '/pages/goods_cate/goods_cate') {
					let data = this.$util.stringIntercept(item.url, 1, "\?")
					data = this.$util.stringIntercept(data, 1, "\=")
					uni.setStorageSync('storeIndex', data);
					uni.switchTab({
						url: '/pages/goods_cate/goods_cate'
					})
				} else {
					uni.navigateTo({
						url: item.url
					})
				}
			},
			
			// 品牌好店
			storeMerchant() {
				if (this.loadend) return;
				storeMerchantList({
					page: this.page,
					limit: this.limit,
					from: this.lat + ',' + this.lng,
					is_best: '1',
					is_bendi: 1
				}).then(res => {
					// console.log(res)
					let list = res.data.list
					let loadend = list.length < this.limit;
					this.loadend = loadend;
					this.loading = false;
					this.loadTitle = loadend ? '已全部加载' : '加载更多';
					this.brandList.push(...list)
					this.page++;
				})
			},
			// 进店
			goStore: function(id) {
				
				uni.navigateTo({
					url: `/pages/store/home/index?id=${id}`
				})
			},
			tabClick (index) {
				this.curTab = index
			}
		},
		onReachBottom() {
			this.storeMerchant()
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f2f5fd;
	}
	
	.mp-header {
		z-index: 999;
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		padding-bottom: 20rpx;
		background-color: #4c5574;
		box-shadow: unset;
		.serch-wrapper {
			height: 42px;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx 0 30rpx;
			.item {
				width: 33%;
			}
			.title {
				text-align: center;
				font-size: 32rpx;
				color: #fff;
			}
			image {
				width: 120rpx;
				height: 42rpx;
				margin-right: 30rpx;
			}
			.input,.uninput {
				flex: 1;
				display: flex;
				align-items: center;
				width: 600rpx;
				height: 58rpx;
				padding: 0 0 0 30rpx;
				background: rgba(247, 247, 247, 1);
				border: 1px solid rgba(241, 241, 241, 1);
				border-radius: 29rpx;
				color: #BBBBBB;
				font-size: 28rpx;
				
				.iconfont {
					margin-right: 20rpx;
				}
				
			}
			.uninput{
				min-width: 460rpx;
				max-width: 480rpx;
			}
			.locat {
				flex: 1;
				justify-content: center;
				text {
					display: block;
					max-width: 130rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}
	}
	.bg_area {
		width: 100%;
		padding: 0 20rpx;
		background-color: #4c5574;
		.hot-search {
			display: flex;
			width: 100%;
			height: 60rpx;
			padding-bottom: 20rpx;
			.title {
				width: 90rpx;
				text-align: center;
				color: #fff;
				font-weight: 600;
				font-size: 30rpx;
			}
			.scroll-content {
				width: calc(100% - 100rpx);
				height: 100%;
				.scroll-wapper {
					width: 100%;
					white-space: nowrap;
				}
				.hot_tag {
					padding: 0 20rpx;
					background-color: #3a4159;
					border-radius: 20rpx;
					color: #fff;
					margin-left: 20rpx;
					font-size: 30rpx;
				}
			}
		}
		.swiper {
			width: 100%;
			height: 450rpx;
			margin-top: 20rpx;
			.swiper-item {
				width: 100%;
				image {
					width: 100%;
					border-radius: 10rpx;
				}
			}
		}
		.nav {
			padding: 0 0rpx 30rpx;
			flex-wrap: wrap;
			margin-top: 0;
			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				width: 20%;
				margin-top: 30rpx;
				color: #fff;
				image {
					width: 86rpx;
					height: 86rpx;
					border-radius: 50%;
					border: 1px solid #d8a9fa;
				}
			}
		}
	}
	.wapper {
		width: 100%;
		padding: 20rpx;
		.menu-group {
			width: 100%;
			background-color: #fff;
			border-radius: 30rpx;
			.top_menu {
				display: flex;
				.menu_items {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 50%;
					padding: 50rpx 0;
					border-bottom: 1px solid #eae3ec;
					&:not(:last-child){
						border-right: 1px solid #eae3ec;
					}
					.title {
						display: flex;
						flex-direction: column;
						align-items: center;
						text:first-child {
							font-size: 36rpx;
							font-weight: 700;
						}
						text:last-child {
							color: #999;
							font-size: 28rpx;
						}
					}
					image {
						width: 80rpx;
						height: 80rpx;
						margin-left: 20rpx;
					}
				}
			}
			.bottom_menu {
				display: flex;
				.menu_item {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					width: 25%;
					padding: 40rpx 0;
					&:not(:last-child){
						border-right: 1px solid #eae3ec;
					}
					text {
						display: flex;
						flex-direction: column;
						color: #8088a3;
						font-size: 30rpx;
						font-weight: 700;
						margin-bottom: 20rpx;
					}
					image {
						width: 80rpx;
						height: 80rpx;
					}
				}
			}
		}
		.store-wapper {
			width: 100%;
			margin-top: 50rpx;
			.tabs {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 100%;
				margin-bottom: 30rpx;
				text {
					position: relative;
					margin: 0 20rpx;
					font-size: 30rpx;
					font-weight: 700;
					color: #4c5574;
					padding: 0 10rpx;
				}
				.active {
					color: #c24fdf;
					&::after {
						content: '';
						position: absolute;
						bottom: -10rpx;
						left: 0;
						width: 100%;
						height: 7rpx;
						background-color: #c24fdf;
						border-radius: 4rpx;
					}
				}
			}
			
			.hot-search {
				display: flex;
				align-items: center;
				width: 100%;
				height: 64rpx;
				// padding-bottom: 20rpx;
				.title {
					width: 90rpx;
					height: 64rpx;
					line-height: 64rpx;
					text-align: center;
					font-size: 30rpx;
				}
				
				.scroll-wapper {
					// display: flex;
					// align-items: center;
					width: calc(100% - 100rpx);
					height: 64rpx;
					line-height: 64rpx;
					white-space: nowrap;
					overflow-x: auto;
				}
				.hot_tag {
					padding: 0 20rpx;
					border-radius: 20rpx;
					margin-left: 20rpx;
					font-size: 28rpx;
					border: 1px solid #4c5574;
				}
			}
			.store-list {
				width: 100%;
				padding-top: 20rpx;
				.store_item {
					width: 100%;
					background-color: #fff;
					border-radius: 20rpx;
					margin-bottom: 20rpx;
					padding: 25rpx 25rpx 0;
					.details {
						display: flex;
						.left {
							width: 55%;
							.title {
								display: flex;
								text {
									flex: 1;
									font-size: 32rpx;
									font-weight: 700;
									color: #4c5574;
								}
								image {
									width: 50rpx;
									height: 50rpx;
									margin-left: 50rpx;
								}
							}
							.rate {
								margin: 10rpx 0;
							}
							.distance {
								display: flex;
								align-items: center;
								justify-content: space-between;
								color: #4c5574;
								.locat {
									display: flex;
									align-items: center;
								}
								image {
									width: 50rpx;
									height: 50rpx;
								}
							}
							.fav {
								display: flex;
								align-items: center;
								justify-content: space-between;
								margin-top: 10rpx;
								text {
									color: #1a79ee;
								}
								.coupon {
									padding: 4rpx 20rpx;
									border: 1px solid #fd0404;
									color: #fd0404;
									border-radius: 30rpx;
								}
							}
							.comments {
								display: flex;
								margin-top: 10rpx;
								image {
									width: 40rpx;
									height: 40rpx;
									margin-right: 10rpx;
								}
							}
						}
						
						.right_img {
							width: 45%;
							margin-left: 25rpx;
							image {
								width: 100%;
							}
						}
					}
					.tags {
						display: flex;
						padding: 20rpx 0;
						border-bottom: 1rpx solid #babdc9;
						image {
							width: 40rpx;
							height: 40rpx;
							margin-right: 15rpx;
						}
						text {
							margin: 0 15rpx;
						}
					}
					.price {
						display: flex;
						justify-content: space-between;
						margin: 20rpx 0;
						del {
							margin: 0 20rpx;
							color: #999;
							text-decoration: line-through
						}
						view {
							flex: 1;
							display: flex;
							.tips {
								width: 360rpx;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}
						}
					}
					.jifen {
						display: flex;
						align-items: center;
						color: #ff7f01;
						image {
							width: 40rpx;
							height: 40rpx;
							margin-right: 10rpx;
						}
					}
					.more {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 100%;
						height: 100rpx;
						font-size: 30rpx;
						color: #999;
					}
				}
			}
		}
	}
	
	.line2 {
		display: -webkit-box;
		/* autoprefixer: off */
		-webkit-box-orient: vertical;
		/* autoprefixer: on */
		-webkit-line-clamp: 2;
		overflow: hidden;
	}
</style>
